package com.colis.quartz.common.extension;

import com.colis.quartz.common.base.BaseBean;
import com.colis.quartz.common.base.BaseType;
import com.colis.quartz.component.SpringTool;
import org.springframework.util.Assert;

import static com.colis.quartz.common.constant.BaseConst.TYPE_MAP;

/**
 * <p> 描述：基础方法 </p>
 *
 * @author wlp
 * @version 1.0.0
 * @date 2021/12/20 10:45
 */
public abstract class BaseController<T extends BaseBean> {

    protected BaseType<T, BaseJpaRepository<T>> getBaseType(String key) {
        BaseType<T, BaseJpaRepository<T>> type = TYPE_MAP.get(key);
        Assert.notNull(type, "BaseType 未获取到");
        return type;
    }

    protected <S extends BaseJpaService<BaseJpaRepository<T>, T>> S getService(String key) {
        S service = (S) SpringTool.getBean(getBaseType(key).getService().getClass());
        Assert.notNull(service, "service 未获取到bean");
        return service;
    }

    protected <M extends BaseJpaRepository<T>> M getMapper(String key) {
        M mapper = (M) SpringTool.getBean(getBaseType(key).getMClass());
        Assert.notNull(mapper, "mapper 未获取到bean");
        return mapper;
    }

}
